// This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
using Leetcode;
namespace Leetcode.N0385;

public class NestedInteger
{
    private IList<NestedInteger> list;

    // Constructor initializes an empty nested list.
    public NestedInteger()
    {
        this.list = new List<NestedInteger>();
    }

    private int? value;

    // Constructor initializes a single integer.
    public NestedInteger(int value) : this()
    {
        this.value = value;
    }

    // @return true if this NestedInteger holds a single integer, rather than a nested list.
    public bool IsInteger()
    {
        return this.value.HasValue;
    }

    // @return the single integer that this NestedInteger holds, if it holds a single integer
    // Return null if this NestedInteger holds a nested list
    public int GetInteger()
    {
        return this.value ?? 0;
    }

    // Set this NestedInteger to hold a single integer.
    public void SetInteger(int value)
    {
        this.value = value;
    }

    // Set this NestedInteger to hold a nested list and adds a nested integer to it.
    public void Add(NestedInteger ni)
    {
        this.list.Add(ni);
    }

    // @return the nested list that this NestedInteger holds, if it holds a nested list
    // Return null if this NestedInteger holds a single integer
    public IList<NestedInteger> GetList()
    {
        return this.list;
    }

    public override string ToString()
    {
        if (this.IsInteger())
        {
            return this.value.ToString();
        }
        var s = new string[this.list.Count];
        for (int i = 0; i < s.Length; i++)
        {
            s[i] = this.list[i].ToString();
        }
        return Utils.ToString(s);
    }
}